package com.masterclient.app.businessmodule.minemodule.address.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.address.AddressListBean;
import com.eoner.baselibrary.bean.address.UpdateAddressBean;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/08/25
*/
public class EditAddressPresenter extends EditAddressContract.Presenter{

    @Override
    public void getRegionList(String region_id) {
        Observable<AddressListBean> observable = RetrofitUtil.getInstance().getRegionList(region_id);
        Disposable disposable = toSubscribe(observable, new Consumer<AddressListBean>() {
            @Override
            public void accept(@NonNull AddressListBean bean) throws Exception {
                if(bean!=null&&bean.getData()!=null){
                    mView.getAddressList(bean.getData());
                }
            }
        }, new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void updateAddress(String address_id,String a_id, String address, String default_a, String full_name, String mobile) {
        Map<String,String> map = new HashMap<>();
        map.put("id",address_id);
        map.put("a_id",a_id);
        map.put("address",address);
        map.put("default",default_a);
        map.put("full_name",full_name);
        map.put("mobile",mobile);

        Observable<UpdateAddressBean> observable = RetrofitUtil.getInstance().updateAddress(map);

        Disposable disposable = toSubscribe(observable, new Consumer<UpdateAddressBean>() {
            @Override
            public void accept(@NonNull UpdateAddressBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.audateAddressSuccess(bean.getData());
                }else {
                    mView.addAddressFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer());

        mRxManager.add(disposable);
    }


}